package api

import (
	"github.com/gin-gonic/gin"
	"vtrip/global"
	"vtrip/internal/service"
	"vtrip/pkg/app"
	"vtrip/pkg/errcode"
)

type TourismProject struct{}

func NewTourismProject() TourismProject {
	return TourismProject{}
}

// All
// @Summary 获取所有旅游项目
// @Produce json
// @Param token header string true "Token"
// @Router /tourism/project/all [post]
// @Success 200 {object} model.TourismProjectPart "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (tp TourismProject) All(c *gin.Context) {
	// 业务处理
	svc := service.New(c.Request.Context())
	response := app.NewResponse(c)
	projects, err := svc.GetAllTourismProject()
	if err != nil {
		global.Logger.Errorf("svc.GetAllTourismProject err: %v", err)
		response.ToErrorResponse(errcode.GetAllTourismProjectFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"projects": projects,
	})
}

// Add
// @Summary 新增旅游项目
// @Produce json
// @Param token header string true "Token"
// @Param name body string true "项目名称"
// @Router /tourism/project/add [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (tp TourismProject) Add(c *gin.Context) {
	// 参数校验
	param := service.AddTourismProjectRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	err := svc.AddTourismProject(&param)
	if err != nil {
		global.Logger.Errorf("svc.AddTourismProject err: %v", err)
		response.ToErrorResponse(errcode.AddTourismProjectFail)
		return
	}

	response.ToSuccessResponse(nil)
}
